#ifndef WORKTHREAD_H
#define WORKTHREAD_H

#include <QObject>
#include <QThread>
#include <QDebug>

#include "Qt/socketclient.h"


enum work_mode{
    WORK_CLIENT = 0,
    WORK_SERVER ,
    WORK_LOCAL,
    WORK_DOWNLOAD,
};

class workThread : public QObject
{
    Q_OBJECT
public:
    explicit workThread(QObject *parent = nullptr);
    static void stop(void)
    {
        _stop=1;
    }
    static void start(void)
    {
        _stop=0;
    }
    static void mode(const enum work_mode _m)
    {
        _mode = _m;
    }
    static void IP(const QString _hostName, quint16 _port)
    {
        hostName = _hostName;
        port = _port;
    }



public slots:
    //Client();
    void doWork(const QString &parameter) ;//{
//        QString result=parameter;
//        /* ... here is the expensive or blocking operation ... */
//        emit resultReady(result);
//    }

signals:
    void resultReady(const QString &result);
private:
    SocketClient *socket_client;
    static uint8_t _stop;
    static enum work_mode _mode;
    static QString hostName;
    static quint16 port;

    void client(void);
    void download(void);
    void local(void);
};

class ControllerThread : public QObject
{
    Q_OBJECT
    QThread workerThread;
public:
    ControllerThread() {
        //Worker *worker = new Worker;
        worker = new workThread();
        worker->moveToThread(&workerThread);
        connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
        connect(this, &ControllerThread::operate, worker, &workThread::doWork);
        connect(worker, &workThread::resultReady, this, &ControllerThread::handleResults);
        workerThread.start();
    }
    ~ControllerThread() {
        workerThread.quit();
        workerThread.wait();
    }

    void start(void)
    {
        emit operate("start");
    }

    workThread *worker;
public slots:
    void handleResults(const QString &)
    {
        ;
    }
signals:
    void operate(const QString &);
};

#endif // WORKTHREAD_H
